﻿using Core.Infarastructure;
using Core.Infarastructure.Structs;
using Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Generators
{   
    /// <summary>
    /// Provide a logic of generating first collection of stacks
    /// </summary>
    public class StackGenerator
    {
        /// <summary>
        /// Generates the first set of stacks when the game was started
        /// </summary>
        /// <param name="drawingFieldSize"></param>
        /// <param name="userMaxStacksCount"></param>
        /// <returns></returns>
        public List<Stack> GenerateFirstCollection(DrawingFieldStructure drawingFieldSize, int userMaxStacksCount)
        {
            List<Stack> stacks = new List<Stack>();

            List<int> generatedNumbers = new List<int>();

            Random random = new Random();

            var currentCount = random.Next(Constants.MINIMUM_STACK_COUNT, userMaxStacksCount);
            
            while (stacks.Count < Constants.MINIMUM_STACK_COUNT)
            {
                for (var i = 0; i < currentCount; i++)
                {
                    var isExist = false;

                    var randomX = random.Next(Constants.START_POSITION_OF_STACKS, Constants.END_POSITION_OF_STACKS);

                    foreach (var currentNumber in generatedNumbers)
                    {
                        if (currentNumber == randomX)
                        {
                            isExist = true;
                        }
                    }
                    if (isExist == false)
                    {
                        generatedNumbers.Add(randomX);
                        stacks.Add(new Stack(randomX * Constants.STACK_SIZE,
                            drawingFieldSize.Height - Constants.STACK_SIZE) { Falling = false });
                        
                    }

                }
            }
            return stacks;
        }


    }
}
